home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / AHDI / TTFHDX / WINCAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  5.0 KB  |  346 lines

  1. /* wincap.c */
  2.  
  3. /*
  4.  * Winchester Disk `capability' file parser.
  5.  *
  6.  *    int wgetent(name);
  7.  *    char *name;
  8.  *
  9.  *    int wallents(buf);
  10.  *    char *buf;
  11.  *
  12.  *    int wgetnum(id, anum);
  13.  *    char *id;
  14.  *    long *anum;
  15.  *
  16.  *    int wgetflag(id);
  17.  *    char *id;
  18.  *
  19.  *    char *wgetstr(id);
  20.  *    char *id;
  21.  *
  22.  *    int wclose();
  23.  *
  24.  */
  25. #include <obdefs.h>
  26. #include <osbind.h>
  27. #include "fhdx.h"
  28. #include "define.h"
  29. #include "addr.h"
  30.  
  31.  
  32. #define    WBUFSIZE    1024
  33.  
  34. extern char toupper();
  35. extern char *getln();
  36. extern cgetln();
  37.  
  38.  
  39. char *wgetstr();
  40. char *wfindid();
  41.  
  42. static char wbuf[WBUFSIZE];
  43. static int wcapfd;
  44. static int wcapopen = 0;
  45.  
  46.  
  47. /*
  48.  * Get an entry into the buffer.
  49.  *
  50.  */
  51. wgetent(name, id)
  52. char *name;
  53. char *id;
  54. {
  55.     char *nm;
  56.  
  57.     if (wcapok() < 0) return ERROR;
  58.     wreset();
  59.  
  60.     if (id == NULL)
  61.     id = "mn";
  62.  
  63.     while (nextentry() == OK)
  64.     {
  65.     nm = wgetstr(id);
  66.     if (nm != NULL &&
  67.         !strcmp(nm, name)) return OK;
  68.     }
  69.     return ERROR;
  70. }
  71.  
  72.  
  73. /*
  74.  * Seek to beginning of wincap file
  75.  * and reset the line demon.
  76.  *
  77.  */
  78. wreset()
  79. {
  80.     Fseek(0L, wcapfd, 0);
  81.     getln(-1);
  82. }
  83.  
  84.  
  85. /*
  86.  * Get a list of all wcap entries;
  87.  * stuff into the buffer, seperated by nulls;
  88.  * final null marks last entry.
  89.  *
  90.  */
  91. wallents(buf, id)
  92. char *buf;
  93. char *id;
  94. {
  95.     char *nm;
  96.  
  97.     if (id == NULL)
  98.     id = "mn";
  99.  
  100.     if (wcapok() < 0) return ERROR;
  101.     wreset();
  102.  
  103.     while (nextentry() == OK)
  104.     {
  105.     if ((nm = wgetstr(id)) == NULL)
  106.         continue;
  107.     while (*nm)
  108.         *buf++ = *nm++;
  109.     *buf++ = '\0';
  110.     }
  111.  
  112.     *buf++ = '\0';
  113.     return OK;
  114. }
  115.  
  116.  
  117. /*
  118.  * Get number from wcap entry.
  119.  *
  120.  */
  121. int wgetnum(id, anum)
  122. char *id;
  123. long *anum;
  124. {
  125.     char *ent;
  126.  
  127.     if ((ent = wfindid(id)) == NULL ||
  128.     *ent++ != '#')
  129.         return ERROR;
  130.     return getnum(ent, anum);
  131. }
  132.  
  133.  
  134. /*
  135.  * Get flag from wcap entry.
  136.  *
  137.  */
  138. wgetflag(id)
  139. char *id;
  140. {
  141.     char *ent;
  142.  
  143.     if ((ent = wfindid(id)) == NULL)
  144.         return ERROR;
  145.  
  146.     return OK;
  147. }
  148.  
  149.  
  150. /*
  151.  * Get string from wcap entry;
  152.  * return NULL if the id doesn't exist.
  153.  *
  154.  */
  155. char *wgetstr(id)
  156. char *id;
  157. {
  158.     char *ent;
  159.  
  160.     if ((ent = wfindid(id)) == NULL ||
  161.     *ent++ != '=')
  162.         return NULL;
  163.  
  164.     return ent;
  165. }
  166.  
  167.  
  168. /*
  169.  * Find an id;
  170.  * return pointer to rest of item (or NULL).
  171.  *
  172.  */
  173. char *wfindid(id)
  174. char *id;
  175. {
  176.     char *s;
  177.     char *skipstr();
  178.  
  179.     s = &wbuf[0];
  180.     s = skipstr(s);
  181.     while (*s)
  182.     {
  183.     if (s[0] == id[0] &&
  184.         s[1] != '\0' && id[1] != '\0' &&
  185.         s[1] == id[1])
  186.         return (s + 2);
  187.     s = skipstr(s);
  188.     }
  189.     return NULL;
  190. }
  191.  
  192.  
  193. /*
  194.  * Skip string;
  195.  * return ptr to thing past the string's `\0'.
  196.  *
  197.  */
  198. char *skipstr(s)
  199. char *s;
  200. {
  201.     while (*s) ++s;
  202.     return ++s;
  203. }
  204.  
  205.  
  206. /*
  207.  * Get next entry from wincap file;
  208.  * return ERROR if no more entries.
  209.  *
  210.  * Seperating `:'s are turned into \0,
  211.  * `\' can prevent that.
  212.  *
  213.  ***************** ***************** ***************** *****************
  214.  * Awww, shucks -- we need to do a "string delete" for that.  So '\'
  215.  * doesn't work yet.
  216.  ***************** ***************** ***************** *****************
  217.  *
  218.  */
  219. nextentry()
  220. {
  221.     char *s;
  222.  
  223.     while (cgetln(wcapfd, wbuf))
  224.     {
  225.     if (wbuf[0] == '#' ||        /* comment or empty line */
  226.         wbuf[0] == '\0')
  227.         continue;
  228.  
  229.     for (s = wbuf; *s; ++s)
  230.         switch (*s)
  231.         {
  232.         case '\\':    ++s;
  233.                 break;
  234.  
  235.         case ':':    *s = '\0';
  236.                 break;
  237.  
  238.         default:    continue;
  239.         }
  240.     *s++ = '\0';
  241.     return OK;
  242.     }
  243.  
  244.     return ERROR;
  245. }
  246.  
  247.  
  248. /*
  249.  * If the wcap file isn't open, then open it;
  250.  * return ERROR if the file won't open.
  251.  *
  252.  */
  253. wcapok()
  254. {
  255.     extern int running;
  256.  
  257.     if (!wcapopen &&
  258.     (wcapfd = (int)Fopen(WCAPFILE, 0)) < 0)
  259.     {
  260.     running = 0;
  261.     return err(nowincap);
  262.     }
  263.  
  264.     wcapopen = 1;
  265.     return OK;
  266. }
  267.  
  268.  
  269. /*
  270.  * If the wcap file isn't closed, then close it;
  271.  * return ERROR if the file won't close.
  272.  *
  273.  */
  274. wclose()
  275. {
  276.     int ret;
  277.  
  278.     ret = OK;
  279.     if (wcapopen)
  280.         if ((ret = Fclose(wcapfd)) == OK)
  281.         wcapopen = 0;
  282.  
  283.     return ret;
  284. }
  285.  
  286.  
  287. /*
  288.  * Parse number;
  289.  *    octal numbers start with a leading `0';
  290.  *    decimal numbers start with digits;
  291.  *    hex numbers start with `0x' or `$';
  292.  *    numbers may be terminated with a `k' (1,000)
  293.  *    or `m' (1,000,000) multiplier.
  294.  *
  295.  */
  296. getnum(s, av)
  297. char *s;
  298. long *av;
  299. {
  300.     static char numtab[] = "0123456789abcdefABCDEF";
  301.     int base, i;
  302.     long v;
  303.  
  304.     base = 10;
  305.     v = 0L;
  306.     if(*s == '$') {
  307.     ++s;
  308.     base = 16;
  309.     } else if (*s == '0' && toupper(s[1]) == 'X') {
  310.     s += 2;
  311.     base = 16;
  312.     } else if (*s == '0')
  313.     base = 8;
  314.  
  315.     while(*s) {
  316.     for(i = 0; numtab[i]; ++i)
  317.         if(*s == numtab[i]) break;
  318.     if(!numtab[i]) break;
  319.     if(base == 16 && i >= 16) i-=6;
  320.     if(i >= base) return ERROR;
  321.     v = (v * base) + i;
  322.     ++s;
  323.     }
  324.  
  325.  
  326.     /* handle multiplier */
  327.     switch (toupper(*s)) {
  328.     case 'K':
  329.         v *= 1024L;
  330.         break;
  331.  
  332.     case 'M':
  333.         v *= (1024L * 1024L);
  334.         break;
  335.  
  336.     case '\0':
  337.         break;
  338.  
  339.     default:
  340.         return ERROR;
  341.     }
  342.  
  343.     *av = v;
  344.     return OK;
  345. }
  346.